import type { Cell, Graph } from "@antv/x6";
import ContextMenu from "@imengyu/vue3-context-menu";

export type MachineClickHandler = (cell: Cell) => void;

export interface ContextMenuOptions {
  machineClick: MachineClickHandler;
}

// 右键菜单
const handleNodeContextMenu = (e: any, cell: Cell, handler?: MachineClickHandler) => {
  ContextMenu.showContextMenu({
    x: e.pageX,
    y: e.pageY,
    items: [
      {
        label: "主机信息",
        disabled: false,
        onClick: () => {
          if (handler) {
            handler(cell);
          }
        }
      }
    ]
  });
};

export function setupContextMenu(graph: Graph, options?: ContextMenuOptions) {
  graph.on("node:contextmenu", ({ e, x, y, cell, view }) => {
    handleNodeContextMenu(e, cell, options?.machineClick);
  });
}
